package com.pac.menu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.LogicException;
import com.common.util.SessionKeyContent;
import com.pac.menu.bean.MenuBean;
import com.pac.menu.bean.MenuTreeBean;
import com.pac.menu.service.MenuService;
import com.pac.user.bean.UserBean;

/**
 * 菜单Controller
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月13日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@Scope("prototype")
@RequestMapping("/menu")
public class MenuController {

	private static Logger logger = Logger.getLogger(MenuController.class);

	@Autowired
	private MenuService menuService;

	/**
	 * 获取菜单树 
	 * 
	 * @param request
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/menuTree")
	@ResponseBody
	public Map<String, Object> menuTree(HttpServletRequest request) {
		HttpSession session = request.getSession();
		UserBean userBean = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);

		List<MenuBean> menuBeanList = null;
		try {
			menuBeanList = menuService.menuTree(userBean.getId());
		} catch (LogicException ex) {
			logger.error("查询菜单出错：" + ex.getMessage());
		} catch (Exception ex) {
			logger.error("查询菜单出错：" + ex);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menus", menuBeanList);

		return map;
	}

	/**
	 * 构建菜单树, 角色管理使用
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public List<MenuTreeBean> getMenuTree() {
		List<MenuTreeBean> menuTreeBeanList = menuService.createMenuTree();

		return menuTreeBeanList;
	}

}
